//
//  TCLYBabyHomeViewController.m
//  YuErmuYing
//
//  Created by LYang on 16/9/8.
//  Copyright © 2016年 Tiancheng. All rights reserved.
//
/***************************************************************************************这个是宝妈商城的首页*********************************************************************************************************/

#import "TCLYBabyHomeViewController.h"
#import "TCLYIntegralTableViewCell.h"
#import "TCLYIntegralCollectionViewCell.h"
#import "TCLYTwoIntegralCollectionViewCell.h"
#import "TCLYBabyShopView.h"
#import "TCLYHomeModel.h"
#import "Macros.h"
#import "Masonry.h"
#import "UIImageView+WebCache.h"
#import "TCLYBabyGoodsViewController.h"

#import "TCLYBabyAllGoodsViewController.h"
@interface TCLYBabyHomeViewController ()<UITableViewDataSource, UITableViewDelegate, UICollectionViewDataSource,UICollectionViewDelegate>

@property (nonatomic, strong) TCLYHomeModel * homeModel;
@property (nonatomic, strong) TCLYBabyShopView * babyShopView;
@property (nonatomic, strong) UITableView * babyHomeTabelView;
@property (nonatomic, strong) NSMutableArray * imageArr;
@property (nonatomic, copy) NSMutableArray * goodsArr;
@end

@implementation TCLYBabyHomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Do any additional setup after loading the view.
    
    self.navigationController.navigationBarHidden = YES;
    self.tabBarController.tabBar.hidden = YES;
    self.view.backgroundColor = [UIColor whiteColor];
    UIView * titleView = [[UIView alloc] init];
    titleView.frame = CGRectMake(0, 0, screenWidth, 20);
    titleView.backgroundColor = statusColor;
    
    [self.view addSubview:titleView];
    
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(babyCarouse) name:@"babyCarouse" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loadBabyHome) name:@"loadBabyHome" object:nil];
    
    self.homeModel = [[TCLYHomeModel alloc] init];
    [self.homeModel loadBabyHome];
}
-(void)loadBabyHome{
//    NSLog(@"收到通知");
    self.goodsArr = self.homeModel.babyAbroadArr;
    self.imageArr = [NSMutableArray array];
    //广告页面的滚播图片
    for (int i = 0; i<self.homeModel.babyCarouselArr.count; i++) {
        UIImageView * imageView = [[UIImageView alloc] init];
        NSURL * url = [NSURL URLWithString:self.homeModel.babyCarouselArr[i]];
        [imageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"LYJia1"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            [self.imageArr addObject:imageView.image];
            if (self.imageArr.count == self.homeModel.babyCarouselArr.count) {
                [[NSNotificationCenter defaultCenter] postNotificationName:@"babyCarouse" object:self];
            }
        }];
    }
    //刷新tabelView的方法
    [self.babyHomeTabelView reloadData];
}
//广告页面加载完事后创建视图
-(void)babyCarouse{
//    NSLog(@"%@",self.imageArr);
    [self createView];
    //    [self.integralTabelView reloadData];
}

-(void)createView{
    self.babyShopView = [[TCLYBabyShopView alloc] initWithFrame:CGRectMake(0, 0, screenWidth, 250/667.0*screenHeight) ImageArr:self.imageArr];
    [self.babyShopView.overseasBtn addTarget:self action:@selector(headerBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.babyShopView.domesticBtn addTarget:self action:@selector(headerBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    
    self.babyHomeTabelView = [[UITableView alloc] init];
    self.babyHomeTabelView.frame = CGRectMake(0, 20, screenWidth, screenHeight);
    self.babyHomeTabelView.tableHeaderView = self.babyShopView;
    self.babyHomeTabelView.backgroundColor = bgColor;
    [self.babyHomeTabelView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    self.babyHomeTabelView.delegate = self;
    self.babyHomeTabelView.dataSource = self;
    [self.view addSubview:self.babyHomeTabelView];
    
    UIButton * leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [leftBtn setImage:[UIImage imageNamed:@"left"] forState:UIControlStateNormal];
    [leftBtn addTarget:self action:@selector(leftBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.babyShopView addSubview:leftBtn];
    [leftBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.left.mas_equalTo(12);
        make.width.mas_equalTo(20);
        make.height.mas_equalTo(30);
    }];
    
}

//tableView必须实现的两个代理
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.goodsArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString * identifier = @"babyTabelCell";
//    TCLYIntegralTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:identifier];
//    if (!cell) {
//        cell = [[TCLYIntegralTableViewCell alloc] initWithReuseIdentifier:identifier];
//    }
    TCLYIntegralTableViewCell * cell = [[TCLYIntegralTableViewCell alloc] initWithReuseIdentifier:identifier];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.titleLabel.text = self.homeModel.babyClassifyArr[indexPath.row][@"cat_name"];
    
    cell.moreBtn.tag = 1000+indexPath.row;
    
    [cell.moreBtn addTarget:self action:@selector(moreBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    cell.oneCollectionView.dataSource = self;
    cell.oneCollectionView.delegate = self;
    cell.oneCollectionView.tag = 10000+indexPath.row;
    cell.twoCollectionView.dataSource = self;
    cell.twoCollectionView.delegate = self;
    cell.twoCollectionView.tag = 20000+indexPath.row;
    cell.backgroundColor = bgColor;
    

    
    return cell;
}
//改变行的高度（实现主个代理方法后 rowHeight 设定的高度无效）
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 350/667.0*screenHeight;
}
//collectionView必须实现的代理
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if (collectionView.tag < 20000) {
        return 2;
    }
    if (collectionView.tag >= 20000) {
        return 4;
    }
    return 0;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    if ( 20000 > collectionView.tag) {
        static NSString * identifier = @"integralCell";
        TCLYIntegralCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
        cell.brandLabel.text = self.goodsArr[collectionView.tag-10000][indexPath.row][@"brand"];
        cell.nameLabel.text = self.goodsArr[collectionView.tag-10000][indexPath.row][@"goods_name"];
        cell.introduceLabel.text = self.goodsArr[collectionView.tag-10000][indexPath.row][@"standard"];
        NSURL * url = [NSURL URLWithString:self.goodsArr[collectionView.tag-10000][indexPath.row][@"goods_pic"]];
        [cell.goodsImageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"LYJia2"]];
        
        return cell;
    }
    if (collectionView.tag >= 20000) {
        static NSString * identifier = @"twoIntegralCell";
        TCLYTwoIntegralCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
        cell.nameLabel.text = self.goodsArr[collectionView.tag-20000][indexPath.row+2][@"goods_name"];
        cell.introduceLabel.text = self.goodsArr[collectionView.tag-20000][indexPath.row+2][@"standard"];
        NSURL * url = [NSURL URLWithString:self.goodsArr[collectionView.tag-20000][indexPath.row+2][@"goods_pic"]];
        [cell.goodsImageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"bgHeader"]];
        return cell;
    }
    return 0;
}
//点击collectionCell产生的事件
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    NSLog(@"col%ld",(long)indexPath.row);
    NSLog(@"tabel%ld",collectionView.tag);
    if (collectionView.tag < 20000) {
        TCLYBabyGoodsViewController * babyGoodsViewCtr = [[TCLYBabyGoodsViewController alloc] init];
        babyGoodsViewCtr.parameterDic = self.goodsArr[collectionView.tag-10000][indexPath.row];
        NSLog(@"%@",self.goodsArr[collectionView.tag-10000][indexPath.row][@"goods_id"]);
        babyGoodsViewCtr.isBabyHome = YES;
        [self.navigationController pushViewController:babyGoodsViewCtr animated:YES];
    }
    
    if (collectionView.tag >= 20000) {
        TCLYBabyGoodsViewController * babyGoodsViewCtr = [[TCLYBabyGoodsViewController alloc] init];
        babyGoodsViewCtr.parameterDic = self.goodsArr[collectionView.tag-20000][indexPath.row+2];
        babyGoodsViewCtr.isBabyHome = YES;
        [self.navigationController pushViewController:babyGoodsViewCtr animated:YES];
        
    }
}
//返回按钮
-(void)leftBtnAction:(id) sender{
    [self.navigationController popToRootViewControllerAnimated:YES];
    self.tabBarController.tabBar.hidden = NO;
}
//国内淘和海外购的两个按钮的事件
-(void)headerBtnAction:(UIButton *)sender{
    if ([(UIButton *)sender isEqual:self.babyShopView.overseasBtn]) {
        self.goodsArr = self.homeModel.babyAbroadArr;
        [self.babyHomeTabelView reloadData];
    };
    if ([(UIButton *)sender isEqual:self.babyShopView.domesticBtn]) {
        self.goodsArr = self.homeModel.babyHomeArr;
        [self.babyHomeTabelView reloadData];
    }
}
//更多的按钮  点击进入到分类的详细界面
-(void)moreBtnAction:(id)sender{
    TCLYBabyAllGoodsViewController * allGoodsViewCtr = [[TCLYBabyAllGoodsViewController alloc] init];
    
    allGoodsViewCtr.allGoodsParameterDic = self.homeModel.babyClassifyArr[((UIButton *)sender).tag-1000];
    
    allGoodsViewCtr.allGoodsParametersDic = self.goodsArr[((UIButton *)sender).tag-1000][0];
//    NSLog(@"%@后面%@",allGoodsViewCtr.allGoodsParameterDic,allGoodsViewCtr.allGoodsParametersDic);
    [self.navigationController pushViewController:allGoodsViewCtr animated:YES];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
